// deep copy
function deepCopy(source){
    var res;
    if (typeof source != "object") {
        res = source;
        return res;
    }
    var res = Array.isArray(source) ? [] : {};
    for (var key in source) {
        if (typeof source != "object") {
            res[key] = source[key];
        } else {
            res[key] = deepCopy(source[key]);
        }
    }
    return res;
}

// 检测用户是否修改数据
function isChanged(backup, newData){
    var sign = false; // 默认相等（不提交修改）
    if (typeof newData != "object" && typeof backup != "object") { // 基本类型
        return sign = newData==backup ? false : true;
    } else { // 引用类型
        for (var key in newData) {
            if (newData[key] != backup[key]) {
                sign = true; // 数据变动，提交修改
                break;
            }
        }
        return sign;
    }
}

// 日期格式化
function getDate(dt, sign = false){
  // sign [true | false]
  var date = new Date(dt);
  var year = date.getFullYear();
  var month = (date.getMonth()+1).toString().padStart(2, '0');
  var day = (date.getDate()).toString().padStart(2, '0');
  var week = date.getDay(); // 周
  var hours = date.getHours().toString().padStart(2, '0');
  var minutes = date.getMinutes().toString().padStart(2, '0');
  var seconds = date.getSeconds().toString().padStart(2, '0');
  var milliSeconds = date.getMilliseconds().toString().padStart(3, '0');

  var dateInfo = '';
  if(sign){
      dateInfo = `${year}-${month}-${day}- ${hours}:${minutes}:${seconds}`;
  } else {
      dateInfo = `${year}-${month}-${day}`;
  }
  return dateInfo;
}

// OutPut
const utils = {
    deepCopy,
    isChanged,
    getDate
};

export default utils;